<template>
  <a-drawer
    :faceOrderPrintDto="faceOrderPrintDto"
    title="电子面单"
    :maskClosable="false"
    width=420
    placement="right"
    :closable="false"
    @close="onClose"
    :visible="faceOrderVisiable"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <div>
      <div id="printTest" v-html="printO" style="align-content: center">
      </div>
    </div>

    <div class="drawer-bootom-button">
      <a-popconfirm title="确定放弃编辑？" @confirm="onClose" okText="确定" cancelText="取消">
        <a-button style="margin-right: .8rem">取消</a-button>
      </a-popconfirm>
      <a-button type="primary" v-print="'#printTest'">开始打印</a-button>
    </div>
  </a-drawer>
</template>
<script>
export default {

  name: 'FaceOrder',
  props: {
    faceOrderVisiable: {
      default: false
    }
  },
  data () {
    return {
      options: [],
      loading: false,
      printO: '',
      orderCode: '',
      AllList: {}, // 打印电子面单接口参数
      sender: {},
      faceOrderPrintDto: {
        commodity: [
          {
            goodsName: '',
            goodsVol: 1.0,
            goodsWeight: 0,
            goodsquantity: 0
          }
        ],
        cost: 2,
        deliveryMethod: 1,
        expType: 1,
        isNotice: 0,
        isReturnPrintTemplate: 0,
        orderCode: '',
        otherCost: 0,
        payType: 1,
        quantity: 1,
        receiver: {
          address: '详细地址',
          cityName: '收货人城市',
          company: '收货公司',
          expAreaName: '区',
          mobile: '收货人电话',
          name: '收货人姓名',
          postCode: '123456',
          provinceName: '收货人省份'
        },
        remark: '订单备注',
        sender: {
          address: '',
          cityName: '',
          company: '',
          expAreaName: '',
          mobile: '',
          name: '',
          postCode: '',
          provinceName: ''
        },
        shipperCode: 'YZPY',
        volume: 0,
        weight: 0
      }
    }
  },
  methods: {
    setAllValues (data) {
      // 获取订单信息
      this.$api.order.findMbaInfo(data.mba00).then((r) => {
        console.info("訂單信息：",r)
        this.AllList = r.data.data
        console.log(this.AllList)
        this.faceOrderPrintDto.commodity[0].goodsName = this.AllList.mbbFileList[0].mbb04
        this.faceOrderPrintDto.commodity[0].goodsquantity = this.AllList.mbbFileList[0].mbb07
        this.faceOrderPrintDto.orderCode = this.AllList.mbaFile.mba01
      })
      // 獲取运费模板
      this.$api.order.findmcbDetailInfo(data.mcb00).then((r) => {
        console.info("運費信息：",r)
        this.sender = r.data.data
        this.faceOrderPrintDto.sender.address = this.sender.address
        this.faceOrderPrintDto.sender.cityName = this.sender.cityName
        this.faceOrderPrintDto.sender.company = this.sender.company
        this.faceOrderPrintDto.sender.expAreaName = this.sender.expAreaName
        this.faceOrderPrintDto.sender.mobile = this.sender.mobile
        this.faceOrderPrintDto.sender.name = this.sender.sendName
        this.faceOrderPrintDto.sender.postCode = this.sender.postCode
        this.faceOrderPrintDto.sender.provinceName = this.sender.provinceName
      })
      setTimeout(() => {
        this.loading = true
        this.md()
      }, 2000)
    },
    setOrderCode (orderCode) {
      this.orderCode = orderCode
      // 已发货查看电子面单
      this.$api.order.OrderTemplate(this.orderCode).then((r) => {
        console.log(r)
        this.printO = r.data.data.printTemplate
      })
    },
    reset () {
      this.loading = false
    },
    onClose () {
      this.reset()
      this.faceOrderPrintDto.orderCode = ''
      this.orderCode = ''
      this.printO = ''
      this.$emit('close')
    },
    // 未发货打印电子面单
    md () {
      console.info("打印傳輸參數：",this.faceOrderPrintDto)
      console.log(this.sender)
      this.$api.order.faceOrder({...this.faceOrderPrintDto}).then((r) => {
        console.info("面單返回結果：", r)
        let data = JSON.parse(r.data.data)
        console.info("結果轉爲json:", data)
        this.printO = data.PrintTemplate
        this.$message.success('发货成功,请打印电子面单')
        this.AllList.mbaFile.mba09 = 2 // 订单状态改为已发货
        this.AllList.mbaFile.mba11 = data.Order.LogisticCode // 订单中物流单号回填
        // 物流公司按重量还是体积收费->计算运费
        if (this.sender.mcb02 === 0) {
          this.AllList.mbaFile.mba05 = ((this.AllList.mbaFile.mba31 <= this.sender.mcb03) ? this.sender.mcb04 : (parseFloat(this.sender.mcb04) + parseFloat(this.sender.mcb05)))
        } else if (this.sender.mcb02 === 1) {
          this.AllList.mbaFile.mba05 = ((this.AllList.mbaFile.mba32 <= this.sender.mcb06) ? this.sender.mcb07 : (parseFloat(this.sender.mcb07) + parseFloat(this.sender.mcb08)))
        }
        this.$api.order.putMba(this.AllList.mbaFile).then((r) => {
          // 发货成功后，修改mbaFile
        })
      })
    }
  }
}
</script>
